<template>
  <Dialog v-model="dialogVisible" :title="t('pickBatch.index.setTemplate.title')">
    <el-form-item
      v-if="data"
      :label="t('pickBatch.index.setTemplate.uploadLabel')"
      label-position="top"
    >
      <IMRUpload v-model="data.applyTemplateUrl" :limit="1" :file-type="['doc', 'xls']" />
    </el-form-item>

    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="submitLoading">{{
        t('common.ok')
      }}</el-button>
      <el-button @click="dialogVisible = false">{{ t('common.cancel') }}</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { PickBatchApi, PickBatchVO } from '@/api/recruit/pickbatch'

defineOptions({ name: 'SetTemplateForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const data = ref<PickBatchVO>()
const dialogVisible = ref(false)
const submitLoading = ref(false)

// 打开弹窗
const open = (value: PickBatchVO) => {
  data.value = value.applyTemplateUrl ? { ...value } : { ...value, applyTemplateUrl: '' }
  dialogVisible.value = true
}

// 提交表单
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  if (!data.value) return
  if (!data.value.applyTemplateUrl) {
    message.error('请上传申请表模板')
    return
  }
  submitLoading.value = true
  try {
    dialogVisible.value = false
    await PickBatchApi.updatePickBatch(data.value)
    message.success(t('common.updateSuccess'))
    // 发送操作成功的事件
    emit('success')
  } finally {
    submitLoading.value = false
  }
}

defineExpose({
  open
})
</script>

<style scoped lang="scss"></style>
